from email import charset

import MySQLdb
from MySQLdb.cursors import DictCursor

conn = MySQLdb.connect(host='123.56.80.63',
                       port=3306,
                       user='root',
                       passwd='mysql',
                       db='hrs',
                       charset='utf8')

try:

    with conn.cursor(DictCursor) as cursor:
        aff = cursor.execute(
            # insert
            # 'insert into tb_dept values (%s,%s,%s)',
            # (90, '研发部', '天津')
            'select dno,dname from tb_dept '
        )
        for x in cursor.fetchall():
            print(x)
        if aff:
            print('ok')
        conn.commit()
except:
    conn.rollback()
